/**************************************************************************/
/*                                                                        */
/*  System  Name :  ICIM                                                  */
/*                                                                        */
/*  Description  :  Dept_data Management                                  */
/*                                                                        */
/*  MODIFICATION HISTORY                                                  */
/*    Date     Ver     Name          Description                          */
/* ---------- ----- ----------- ----------------------------------------- */
/* 2017/09/08 N0.00                Initial release                        */
/*                                                                        */
/*                                                                        */
/** *********************************************************************** */
$(function() {
    var _NORMAL = "0000000";
    $("form").submit(function() {
        return false;
    });

    var isAll = true; //是否为查询所有数据；//默认值
    var colNames = [ BOX_ID_TAG, BOX_STAT_TAG, MDL_ID_TAG, WO_ID_TAG, PRD_QTY_TAG, PRD_GRADE_TAG, "包装人员","包装时间"]
    // "区域代码", "指令状态" ,
    // 		 "传送状态", "从区域代码", "至机台代码", "至从端口代码", "至从区域代码"];
    var itemInfoCM = [ {
        name : 'box_id',
        index : 'box_id',
        label :"box_id",
        width : 180
    }, {
        name : 'box_stat',
        index : 'box_stat',
        label :"box_stat",
        width : 150
    },/* {
        name : 'prd_qty',
        index : 'prd_qty',
        label :"prd_qty",
        width : 100,
        hidden : false
    },*/{
        name : 'mdl_id_fk',
        index : 'mdl_id_fk',
        //	label : nx_toolg_id_fk,
        width : 200
    },{
        name : 'wo_id_fk',
        index : 'wo_id_fk',
        //	label : nx_toolg_id_fk,
        width : 200,
    },{
        name : 'prd_qty',
        index : 'prd_qty',
        //	label : nx_toolg_id_fk,
        width : 100,
    },{
        name : 'prd_grade_fk',
        index : 'prd_grade_fk',
        //	label : nx_toolg_id_fk,
        width : 120,
    },{
        name : 'evt_usr',
        index : 'evt_usr',
        //	label : nx_toolg_id_fk,
        width : 120,
    } ,{
            name : 'evt_timestamp',
            index : 'evt_timestamp',
            //	label : nx_toolg_id_fk,
            width : 150,
        }/*{
        name : 'box_cate',
        index : 'box_cate',
        label :"box_cate",
        width : 120
    },*/ 	/*{
        name : 'box_set_code',
        index : 'box_set_code',
        label : "box_set_code",
        width : 120,
        hidden : false
    },*/  /*{
        name : 'valid_flg',
        index : 'valid_flg',
        label :"valid_flg",
        width : 100,
        editable : false

    },*//* {
        name : 'box_clean_flg',
        index : 'box_clean_flg',
        width : 100,
    },*/ /*{
        name : 'tool_id_fk',
        index : 'tool_id_fk',
        //	label : nx_tool_id_fk,
        width : 150
    },*/ /*{
        name : 'tool_port_id_fk',
        index : 'tool_port_id_fk',
        //	label : ext_9,
        width : 100
    }, *//*{
        name : 'so_id_fk',
        index : 'so_id_fk',
        //	label : nx_toolg_id_fk,
        width : 160
    } */ ];
    // alert("this is notice.js .111.2. ");
    $("#dataListGrd").jqGrid({
        url:"",
        datatype:"local",
        mtype:"POST",
        autowidth:true,
        shrinkToFit:true,
        scroll:false,
        rownumbers : true,
        resizable : true,
        loadonce:true,
        fixed:true,
        viewrecords:true,
        pager : '#dataListPg',
        colModel : itemInfoCM,
        colNames : colNames,
        multipleSelect:true,
        gridComplete : function() {
        }
    });
    // 页面高度变化时，自动调整Grid大小
    var resetJqgrid = function() {
        $(window).unbind("onresize");
        $("#dataListGrd").setGridWidth($("#dataListDiv").width() * 1);
        $("#dataListGrd").setGridHeight($("#dataListDiv").height() * 1.6);
        $(window).bind("onresize", this);
    };
    $('[data-toggle="tooltip"]').tooltip();
    function initFnc() {
        resetJqgrid();
        iniConditionCate();
        // 分页控件 高度控制
        $("#dataListPg").css({
            "height" : "30px"
        });
        $(".ui-pg-selbox").css({
            "height" : "28px"
        });
    }
    initFnc();
    function iniConditionCate() {
        comAddValueByDataCateFnc("#z-dataCateSel","QYCD","ext_1","data_ext",true);
    }
//利用id 传递boxcate参数 抓取资料
    function get_table_value(){
        var dataCateSel= $("#z-dataCateSel").find("option:selected").val();
        var dataValSel = $("#z-dataValSel").find("option:selected").val();
        var iary = [ {

            query_codition : dataCateSel,
            query_value : dataValSel

        }];
        var inTrxObj = {
            trx_id : "FIPINQBOX",
            action_flg : 'V',
            iary : iary
        };
        var outTrxObj = comTrxSubSendPostJson(inTrxObj);
        if (outTrxObj.rtn_code == _NORMAL) {
            setGridInfo(outTrxObj.oary_box, "#dataListGrd", false, true); // comfunction.js中定义的一个函数
        }
    }
    //获取查询条件值的List
    function getDataValFnc(dataCateSel){
        $("#z-dataValSel").empty();
        $("#z-dataValSel").append("<option value=''></option>");
        if (dataCateSel.trim() === 'WO_ID_FK') {
            getWoIdListFnc();
        }else if (dataCateSel.trim() === 'BOX_CATE') {
            getBoxCateFnc();
        }else if (dataCateSel.trim() === 'BOX_CLEAN_FLG') {
            getBoxClnFlgFnc();
        }else if (dataCateSel.trim() === 'BOX_STAT') {
            getBoxStatFnc();
        }
    }
    function getWoIdListFnc(){
        var inTrx = {
            trx_id : 'FDPAPLYWO',
            action_flg : 'Q',
            iary : []
        }
        var outTrx = comTrxSubSendPostJson(inTrx);
        if (outTrx.rtn_code === "0000000") {
            SelectDom .addSelectArr($("#z-dataValSel"), outTrx.oary, "wo_id","wo_id","",true);
        }
    }
    function getBoxCateFnc(){
        SelectDom.initWithSpace($("#z-dataValSel"));
        comAddValueByDataCateFnc("#z-dataValSel","BXCT","data_ext","data_desc",false);
    }
    function getBoxStatFnc(){
        SelectDom.initWithSpace($("#z-dataValSel"));
        comAddValueByDataCateFnc("#z-dataValSel","BXST","data_ext","data_desc",false);
    }
    function getBoxClnFlgFnc(){
        SelectDom.initWithSpace($("#z-dataValSel"));
        $("#z-dataValSel").append("<option value='Y'>Y</option>");
        $("#z-dataValSel").append("<option value='N'>N</option>");
    }

    //下拉匡改变事件 同时将value显示在文本
    $("#z-dataCateSel").change(function(){
        var dataCateSel = $("#z-dataCateSel").find("option:selected").val();
        getDataValFnc(dataCateSel);
    })
    var text  = [];
    var flage = true;
    var mar = [];
    $('#query_btn').click(function(){
        //var inTrxStr = JSON.stringify(text);
        //console.log(inTrxStr);[]
        var condition_arr = new Array();
        var iary = new Array();
        for(var i = 0;i<text.length;i ++ ){
            if(i == 0){
                condition_arr.push(text[i].query_codition)
            }else{
                for(var a =0;a<condition_arr.length;a++){
                    if(text[i].query_codition != condition_arr[a]){
                        condition_arr.push(text[i].query_codition);
                        break;
                    }
                }
            }
        }

        for(var j = 0;j<condition_arr.length;j ++) {
            var condition_id = condition_arr[j];
            var count = 0;
            var conditon_val="";
            for (var k = 0; k < text.length; k++) {
                if (count == 0) {
                    if (text[k].query_codition == condition_arr[j]) {
                        conditon_val += text[k].query_value ;
                        count++;
                    }
                } else {
                    if (text[k].query_codition == condition_arr[j]) {
                        conditon_val += "," + text[k].query_value;
                        count++;
                    }
                }

            }
            var info = {
                query_codition : condition_arr[ j],
                query_value : conditon_val
            };
            iary.push(info);
            // info[condition_arr[ j]] = conditon_val;
        }
        var inTrxObj = {
            trx_id : "FIPINQBOX",
            action_flg : 'V',
            iary : iary
        };
        var outTrxObj = comTrxSubSendPostJson(inTrxObj);
        if (outTrxObj.rtn_code == _NORMAL) {
            setGridInfo(outTrxObj.oary_box, "#dataListGrd", false, true); // comfunction.js中定义的一个函数
        }
        // console.log(mar);
//			console.log(values.substring(0,values.length-1));

    });
    //点击下拉框时触发事件
    $('#z-dataValSel').change(function(){

        var criteriaObj = new Object();
        var zdataCateSel= $("#z-dataCateSel").find("option:selected").val();
        var zdataCateSelT= $("#z-dataCateSel").find("option:selected").text();
        var zdataValSel = $("#z-dataValSel").find("option:selected").val();
        var zdataValSelT = $("#z-dataValSel").find("option:selected").text();
        var data_value = "'"+zdataValSel+"'";
        for(var i = 0;i< text.length;i ++ ){
            if(text[i].query_codition == zdataCateSel && text[i].query_value == data_value  ){
                return false;
            }
        }
        var htmlCon = "<li style='list-style:none;'  class='criteriaLi'>"+
            "<div class='criteriaBox'>"+
            "<span style='display:none'>"+zdataCateSel+"</span>"+
            "<span style='display:none'>"+zdataValSel+"</span>"+
            "<span>"+zdataCateSelT+":</span>"+
            "<span>"+zdataValSelT+"</span>"+
            "<i style='cursor: pointer;' class='glyphicon glyphicon-remove'></i>"+
            "</div>"+
            "</li>";
        flage = isRepeat(text,criteriaObj);
        criteriaObj.query_codition = zdataCateSel;
        criteriaObj.query_value = "'" +  zdataValSel + "'";
        if(flage){
            $('#optionLeft .queryCriteria .criteriaUl').append(htmlCon);
            text.push(criteriaObj);
        }
        console.log(flage);
        console.log(text);
    });
    //数组去重 判断是否有重复的 Object
    function isRepeat(arr,obj){
        for(var i=0; i< arr.length; i++) {
            if(arr[i].query_value == obj.query_value && arr[i].query_codition == obj.query_codition){
                return false;
            }else if(arr[i].query_value != obj.query_value && arr[i].query_codition == obj.query_codition){
                criteriaObj.query_value += arr[i].query_value;
                return true;
            }
        }
        return true;
    }
    //点击标签删除图标触发事件
    $('.criteriaUl').on('click','li div i',function(){
        $(this).parent().remove();
        var lis = $(this).parent();
        var tempObj = new Object();
        var tempDataCateSel = $(this).parent().children('span').eq(0).text();
        var tempDataValSel = $(this).parent().children('span').eq(1).text();
        console.log(tempDataCateSel + tempDataValSel);
        tempObj.query_codition = tempDataCateSel;
        tempObj.query_value = "'"+tempDataValSel+"'";
        for(var i=0; i< text.length; i++){
            if(text[i].query_codition== tempObj.query_codition && text[i].query_value == tempObj.query_value) {
                text.splice(i,1);
                break;
            }
        }
    });
    //判断数值中是否存在相同属性对象
    function isInArray(arr,value,index){
        for(var j in arr){
            if(value == arr[j].query_codition && index != j){
                return arr[index].query_value;
            } else {
                return arr[index].query_value;
            }
        }
        return '';
    }

    //表格自适应
    function resizeFnc() {
        var offsetBottom, divWidth;
        divWidth = $("#divLeft").width();
        offsetBottom =  $(window).height() - $("#divLeft").offset().top;
        $("#divLeft").height(offsetBottom * 0.95);
        $("#dataListGrd").setGridWidth(divWidth * 0.99);
        $("#dataListGrd").setGridHeight(offsetBottom * 0.9 - 101);
    };

    resizeFnc();
    $(window).resize(function () {
        resizeFnc();
    });
    
});